package com.ssy.lingxi.report.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.entity.report.MobileCommonlyUsedAppDO;
import com.ssy.lingxi.report.model.constant.MobileAppMenuEnum;
import com.ssy.lingxi.report.model.vo.request.UpdateMemberMobileCommonlyUsedAppVO;
import com.ssy.lingxi.report.model.vo.response.MemberMobileAppVO;
import com.ssy.lingxi.report.model.vo.response.MemberMobileCommonlyUsedAppVO;
import com.ssy.lingxi.report.repository.report.MobileCommonlyUsedAppRepository;
import com.ssy.lingxi.report.service.IMobileCommonlyUsedAppService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 手机端常用App接口实现
 * @author 伍将
 * @date 2021/3/2
 * @version 2.0.0
 */
@Service
public class MobileCommonlyUsedAppServiceImpl implements IMobileCommonlyUsedAppService {

    @Resource
    private MobileCommonlyUsedAppRepository repository;

    /**
     * 获取常用App
     * @author 伍将
     * @date 2021/3/2
     * @param sysUser: 当前登录用户
     * @return 操作结果
     **/
    @Override
    public Wrapper<MemberMobileCommonlyUsedAppVO> getCommonlyUsedApp(UserLoginCacheDTO sysUser) {

        // 查询常用App
        List<MobileCommonlyUsedAppDO> appList = repository.findByMemberIdAndRoleIdOrderBySort(sysUser.getMemberId(), sysUser.getMemberRoleId());

        MemberMobileCommonlyUsedAppVO result = new MemberMobileCommonlyUsedAppVO();

        // 组装常用App
        List<MemberMobileAppVO> commonlyUsedAppList = appList.stream().map(o -> {
            MemberMobileAppVO entity = new MemberMobileAppVO();
            entity.setCode(o.getAppEnum());
            MobileAppMenuEnum menuEnum = MobileAppMenuEnum.getItem(o.getAppEnum());
            if (null != menuEnum) {
                entity.setName(menuEnum.getName());
                entity.setLinkUrl(menuEnum.getLinkUrl());
            }
            entity.setSort(o.getSort());
            return entity;
        }).collect(Collectors.toList());
        result.setCommonlyUsedAppList(commonlyUsedAppList);

        // 组装其他App
        List<MemberMobileAppVO> otherUsedAppList = Arrays.stream(MobileAppMenuEnum.values())
                .filter(menu ->
                        !appList.stream()
                                .map(MobileCommonlyUsedAppDO::getAppEnum)
                                .collect(Collectors.toList())
                                .contains(menu.getCode()))
                .map(o -> {
                    MemberMobileAppVO entity = new MemberMobileAppVO();
                    entity.setCode(o.getCode());
                    entity.setName(o.getName());
                    entity.setLinkUrl(o.getLinkUrl());
                    entity.setSort(o.getSort());
                    return entity;
                }).collect(Collectors.toList());
        result.setOtherUsedAppList(otherUsedAppList);

        return Wrapper.success(result);
    }

    /**
     * 保存常用App
     * @author 伍将
     * @date 2021/3/2
     * @param sysUser: 当前登录用户
     * @param updateListVO: 更新请求参数
     * @return 操作结果
     **/
    @Transactional
    @Override
    public Wrapper<Void> saveCommonlyUsedApp(UserLoginCacheDTO sysUser, List<UpdateMemberMobileCommonlyUsedAppVO> updateListVO) {

        // 删除旧数据
        repository.deleteByMemberIdAndRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());

        // 保存新数据
        if (null != updateListVO && updateListVO.size() > 0) {
            List<MobileCommonlyUsedAppDO> addList = updateListVO.stream().map(o -> {
                MobileCommonlyUsedAppDO entity = new MobileCommonlyUsedAppDO();
                entity.setAppEnum(o.getCode());
                entity.setSort(o.getSort());
                entity.setMemberId(sysUser.getMemberId());
                entity.setRoleId(sysUser.getMemberRoleId());
                return entity;
            }).collect(Collectors.toList());

            repository.saveAll(addList);
        }

        return Wrapper.success();
    }
}
